/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lsi.sgb.dao;

import br.com.lsi.sgb.model.Aluno;
import br.com.lsi.sgb.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.classic.Session;

/**
 *
 * @author Livia
 */
public class AlunoDao {

    Session sessao = null;

    public void addAluno(Aluno aluno) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.save(aluno);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public Aluno buscaAluno(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Aluno a = (Aluno) sessao.get(Aluno.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return a;
    }

    public void atualizarAluno(Aluno aluno) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.update(aluno);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public List<Aluno> list() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        List<Aluno> lista = new ArrayList<Aluno>();
        lista = sessao.createQuery("from Pessoa where DTYPE = 'aluno'").list();
        sessao.getTransaction().commit();
        sessao.close();
        return lista;
    }

    public boolean remove(Aluno aluno) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.delete(aluno);
        sessao.getTransaction().commit();
        sessao.close();
        return true;
    }
    
     public Aluno getAluById(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Aluno p = (Aluno) sessao.get(Aluno.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return p;
    }
}
